 //
//  Preson.m
//  TLock
//
//  Created by lenny on 2017/11/3.
//  Copyright © 2017年 aliyun. All rights reserved.
//
#import <React/RCTLog.h>

#import "T11Plugin.h"


@implementation T11Plugin

RCT_EXPORT_MODULE();

RCT_EXPORT_METHOD(scan){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //发送打开扫描仪的通知
        [[NSNotificationCenter defaultCenter]postNotificationName:@"OpenScan" object:nil];
    });
}
RCT_EXPORT_METHOD(bindMsgChannel){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //绑定消息通道
        [[NSNotificationCenter defaultCenter]postNotificationName:@"bindMsgChannel" object:nil];
    });
}
RCT_EXPORT_METHOD(unbindMsgChannel){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //解绑消息通道
        [[NSNotificationCenter defaultCenter]postNotificationName:@"unbindMsgChannel" object:nil];
    });
}
RCT_EXPORT_METHOD(logoutYK){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //登出怡康服务器
        [YKBusinessFramework equesLogout];
    });
}
RCT_EXPORT_METHOD(loginYK){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //登录怡康服务器
        [[NSNotificationCenter defaultCenter]postNotificationName:@"loginYK" object:nil];
    });
}
RCT_EXPORT_METHOD(toEyes){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //跳转猫眼
        [[NSNotificationCenter defaultCenter]postNotificationName:@"toEyes" object:nil];
    });
}
RCT_EXPORT_METHOD(getVersion:(RCTResponseSenderBlock)callback){
    dispatch_sync(dispatch_get_main_queue(), ^{
        //获取APP版本
        NSDictionary *infoDic = [[NSBundle mainBundle] infoDictionary];
        NSString *currentVersion = [infoDic objectForKey:@"CFBundleShortVersionString"];
        NSLog(@"当前的版本是 %@",currentVersion);
        callback(@[[NSNull null],currentVersion]);
    });
}
-(NSArray *)supportedEvents {
    return@[@"EventReminder_BarcodeScanSuccessForRN",@"EventReminder_CollectionProductSuccessForRN"];
}
- (void)startObserving {
//    [[NSNotificationCenter defaultCenter] addObserver:self
//                                             selector:@selector(CollectionProductSuccessForRNAction:)
//                                                 name:@"CollectionProductSuccessForRN"
//                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(BarcodeScanSuccessForRNAction:)
                                                 name:@"BarcodeScanSuccessForRN"
                                               object:nil];
    
    
}
- (void)BarcodeScanSuccessForRNAction:(id)obj {
    //    [self.bridge.eventDispatcher sendAppEventWithName:@"EventReminder_BarcodeScanSuccessForRN"
    
    dispatch_async(dispatch_get_main_queue(), ^{
        NSString *qrCode = [obj valueForKey:@"object"];
        [self sendEventWithName:@"EventReminder_BarcodeScanSuccessForRN" body:@{@"qrCode": qrCode}];
        
    });
    
}
//- (void)CollectionProductSuccessForRNAction:(NSNotification*)notification {
//    
//    dispatch_async(dispatch_get_main_queue(), ^{
//        [self sendEventWithName:@"EventReminder_CollectionProductSuccessForRN" body:nil];
//    });
//    
//}

- (void)stopObserving {
  
//    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"CollectionProductSuccessForRN" object:nil];
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"BarcodeScanSuccessForRN" object:nil];
}



@end
